package module.map.operations.nodes
{
	import common.operations.BaseOperation;

	import drongo.model.propertys.ArrayProperty;
	import drongo.model.propertys.NumberProperty;
	import drongo.model.propertys.StringProperty;
	import drongo.model.values.CollectionValue;

	import module.map.model.NodeTypes;
	import module.map.model.Propertys;

	/**
	 * 新建一个节点
	 */
	public class NewNodeOperation extends BaseOperation
	{
		private var node:CollectionValue;
		private var oldState:int;
		public function NewNodeOperation()
		{
			super();
		}

		override public function execute():void
		{
			var points:ArrayProperty;
			var point:CollectionValue;
			var index:int;
			switch (this.data.type)
			{
				case NodeTypes.PATH:
				{
					this.node = NodeTypes.CreateNode(this.data.type, this.data.name, this.data.pos.x, this.data.pos.y);
					this.node.Add(new ArrayProperty(Propertys.POINTS));
					this.node.Add(new NumberProperty(Propertys.COUNT,3));
					points = this.node.Get(Propertys.POINTS) as ArrayProperty;
					for (index = 0; index < 3; index++)
					{
						point = NodeTypes.CreatePoint();
						point.Update(Propertys.X, index * 50);
						point.Update(Propertys.Y, 0);
						points.Push(point);
					}
					break;
				}
				case NodeTypes.AREA:
				{
					this.node = NodeTypes.CreateNode(this.data.type, this.data.name, this.data.pos.x, this.data.pos.y);
					this.node.Add(new ArrayProperty(Propertys.POINTS));
					this.node.Add(new NumberProperty(Propertys.AREA_TYPE, this.data.areaType));

					points = this.node.Get(Propertys.POINTS) as ArrayProperty;
					for (index = 0; index < 3; index++)
					{
						point = NodeTypes.CreatePoint();
						point.Update(Propertys.X, index * 20);
						point.Update(Propertys.Y, 0);
						points.Push(point);
					}
					break;
				}
				case NodeTypes.IMAGE:
				{
					this.node = NodeTypes.CreateNode(this.data.type, this.data.name, this.data.pos.x, this.data.pos.y);
					this.node.Add(new StringProperty(Propertys.PATH, this.data.imagePath));
					break;
				}
				case NodeTypes.TOWER:
				{
					this.node = NodeTypes.CreateNode(this.data.type, this.data.name, this.data.pos.x, this.data.pos.y);
					this.node.Add(new NumberProperty(Propertys.TOWER_TYPE, this.data.towerType));
					break;
				}
			}
			this.project.currentMap.nodes.Push(node);
			this.oldState = this.project.currentMap.Get(Propertys.STATE).GetValue();
			this.project.currentMap.Update(Propertys.STATE, 1);
			this.project.currentMap.recorder.link(this);
		}

		override public function redo():void
		{
			this.project.currentMap.nodes.Push(this.node);
			this.project.currentMap.Update(Propertys.STATE, 1);
		}

		override public function undo():void
		{
			this.project.currentMap.nodes.Remove(this.node);
			this.project.currentMap.Update(Propertys.STATE, this.oldState);
		}

		override public function destroy():void
		{
			this.node = null;
			this.data = null;
			this.oldState = 0;
		}
	}
}